<div class="row">
  <div class="col-lg-12 col-md-12">
    <ba-card title="文章列表" baCardClass="with-scroll table-panel">
      <div class="contnet-top-tools">
        <div class="pull-left">
          <div class="btn-group">
            <button type="button"
                    class="btn btn-default active"
                    [ngClass]="{ 'active': getParams.state == 'all'}"
                    (click)="switchState('all')">
              <span>全部</span>
              <span *ngIf="getParams.state == 'all'">({{ articles.pagination.total || 0 }})</span>
            </button>
            <button type="button"
                    class="btn btn-default"
                    [ngClass]="{ 'active': getParams.state == 1 }"
                    (click)="switchState(1)">
              <span>已发布</span>
              <span *ngIf="getParams.state == 1">({{ articles.pagination.total || 0 }})</span>
            </button>
            <button type="button"
                    class="btn btn-default"
                    [ngClass]="{ 'active': getParams.state == 0 }"
                    (click)="switchState(0)">
              <span>草稿</span>
              <span *ngIf="getParams.state == 0">({{ articles.pagination.total || 0 }})</span>
            </button>
            <button type="button"
                    class="btn btn-default"
                    [ngClass]="{ 'active': getParams.state == -1 }"
                    (click)="switchState(-1)">
              <span>回收站</span>
              <span *ngIf="getParams.state == -1">({{ articles.pagination.total || 0 }})</span>
            </button>
          </div>
          <span>&nbsp;</span>
          <div class="btn-group">
            <button type="button" 
                    class="btn btn-default btn-with-icon"
                    (click)="refreshArticles()">
              <i class="ion-refresh"></i>
              <span>刷新</span>
            </button>
            <button type="button" 
                    class="btn btn-default btn-with-icon"
                    (click)="resetGetParams()">
              <i class="ion-trash-b"></i>
              <span>清空搜索条件</span>
            </button>
            <div class="btn-group" dropdown [isDisabled]="!selectedArticles.length">
              <button type="button" 
                      class="btn btn-default btn-with-icon dropdown-toggle"
                      dropdownToggle>
                <i class="ion-ios-list"></i>
                <span>批量操作</span>
              </button>
              <ul class="dropdown-menu" *dropdownMenu>
                <li class="dropdown-item">
                  <a [href]="" (click)="moveToPublished(selectedArticles)">快速发布</a>
                </li>
                <li class="dropdown-item">
                  <a [href]="" (click)="moveToDraft(selectedArticles)">移至草稿</a>
                </li>
                <li class="dropdown-item">
                  <a [href]="" (click)="moveToRecycle(selectedArticles)">移至回收站</a>
                </li>
                <li class="dropdown-item">
                  <a [href]="" (click)="delArticleModal(selectedArticles)">彻底删除</a>
                </li>
              </ul>
            </div>
          </div>
        </div>
        <form class="pull-right form-inline article-get-form"
              [formGroup]="searchForm" 
              (ngSubmit)="searchArticles(searchForm.value)">
          <div class="input-group">
            <input type="text"
                   [formControl]="keyword" 
                   class="form-control with-default-addon" 
                   placeholder="文章标题、描述">
            <span class="input-group-btn">
              <button class="btn btn-default" 
                      type="submit"
                      [disabled]="!searchForm.valid">搜索</button>
            </span>
          </div>
        </form>
        <span class="pull-right">&nbsp;&nbsp;&nbsp;</span>
        <div class="pull-right form-inline">
          <select class="form-control c-select" disabled *ngIf="false">
            <option default>创建时间</option>
            <option>浏览次数</option>
            <option>评论条数</option>
          </select>
          <span>&nbsp;&nbsp;</span>
          <select class="form-control c-select" 
                  [(ngModel)]="getParams.category"
                  (ngModelChange)="getArticles()">
            <option default value="all">所有分类</option>
            <ng-template [ngIf]="categories && categories.data.length">
              <option *ngFor="let cate of categories.data" [ngValue]="cate._id">
                <span *ngIf="cate.level">└</span>
                <span *ngFor="let num of categoryLevelMark(cate.level)">─</span>
                <span>{{ cate.name }}</span>
              </option>
            </ng-template>
          </select>
          <span>&nbsp;&nbsp;</span>
          <select class="form-control c-select" 
                  [(ngModel)]="getParams.tag"
                  (ngModelChange)="getArticles()">
            <option default value="all">所有标签</option>
            <ng-template [ngIf]="tags && tags.data.length">
              <option *ngFor="let tag of tags.data" [ngValue]="tag._id">
                <span>{{ tag.name }}</span>
              </option>
            </ng-template>
          </select>
          <span>&nbsp;&nbsp;</span>
          <select class="form-control c-select" 
                  [(ngModel)]="getParams.public"
                  (ngModelChange)="getArticles()">
            <option value="all" default>所有类型</option>
            <option value="1">公开</option>
            <option value="0">密码</option>
            <option value="-1">私密</option>
          </select>
        </div>
      </div>
      <div class="table-responsive">
        <div class="article-list">
          <table class="table tablehover table-striped table-no-borders black-muted-bg table-enrich">
            <thead class="thead-inverse">
              <tr>
                <th>
                  <ba-checkbox [(ngModel)]="articlesSelectAll" (ngModelChange)="batchSelectChange($event)">
                    <span ba-checkbox-label>
                      <span>&nbsp;</span>
                      <strong>ID</strong>
                    </span>
                  </ba-checkbox>
                </th>
                <th width="30%">文章</th>
                <th>分类目录</th>
                <th width="150">标签</th>
                <th>评论</th>
                <th>喜欢</th>
                <th>日期</th>
                <th>公开</th>
                <th>状态</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <tr *ngIf="!articles || !articles.data.length">
                <td colspan="10">
                  <p class="text-muted text-center article-err-msg" *ngIf="!articles.data.length">
                    <span *ngIf="fetching.article">数据请求中...</span>
                    <span *ngIf="!fetching.article">暂无数据</span>
                  <p>
                </td>
              </tr>
              <ng-template [ngIf]="articles && articles.data.length">
                <tr *ngFor="let article of articles.data" class="article-item">
                  <td class="batch-checkbox">
                    <ba-checkbox [(ngModel)]="article.selected" (ngModelChange)="itemSelectChange()">
                      <span ba-checkbox-label>
                        <span>&nbsp;</span>
                        <strong>{{ article.id }}</strong>
                      </span>
                    </ba-checkbox>
                  </td>
                  <td class="article-content">
                    <div class="content-box">
                      <div class="content-bg" [ngStyle]="{ 
                        'background-image': 'url(' + (article.thumb || '/assets/img/mobile-thumb-carrousel.jpg') +')' 
                      }"></div>
                      <h4 class="title">
                        <a [href]="'//surmon.me/article/' + article.id" target="_blank">
                          <strong>{{ article.title }}</strong>
                        </a>
                        <i class="ion-checkmark text-success" *ngIf="article.state == 1"></i>
                        <i class="ion-edit text-warning" *ngIf="article.state == 0"></i>
                        <i class="ion-trash-b text-danger" *ngIf="article.state == -1"></i>
                        <i class="ion-locked text-danger" *ngIf="article.public != 1"></i>
                      </h4>
                      <small class="description">
                        <span *ngIf="article.description">{{ article.description | truncate : 128 }}</span>
                        <span *ngIf="!article.description && article.content">{{ article.content | truncate : 128 }}</span>
                        <span *ngIf="!article.description && !article.content" class="text-muted">暂无内容</span>
                      </small>
                    </div>
                  </td>
                  <td class="article-category">
                    <ul class="list-unstyled category-list">
                      <li *ngFor="let category of article.category">
                        <i class="ion-ios-folder"></i>
                        <span>&nbsp;</span>
                        <span>{{ category.name }}</span>
                      </li>
                      <li *ngIf="!article.category.length" class="text-muted">
                        <i class="ion-ios-folder"></i>
                        <span>&nbsp;</span>
                        <span>暂无分类</span>
                      </li>
                    </ul>
                  </td>
                  <td class="article-tag">
                    <ul class="list-unstyled tag-list">
                      <li *ngFor="let tag of article.tag">
                        <i class="ion-pricetags"></i>
                        <span>&nbsp;</span>
                        <span>{{ tag.name }}</span>
                      </li>
                      <li *ngIf="!article.tag.length" class="text-muted">
                        <i class="ion-pricetags"></i>
                        <span>&nbsp;</span>
                        <span>暂无标签</span>
                      </li>
                    </ul>
                  </td>
                  <td>
                    <span *ngIf="article.meta.comments">
                      <a [routerLink]="['/comment/post/', article.id]">{{ article.meta.comments }}条评论</a>
                    </span>
                    <span class="text-muted" *ngIf="!article.meta.comments">暂无评论</span>
                  </td>
                  <td>
                    <span *ngIf="article.meta.likes">{{ article.meta.likes }}人喜欢</span>
                    <span class="text-muted" *ngIf="!article.meta.likes">无人喜欢</span>
                  </td>
                  <td>{{ article.create_at | dataToLocale }}</td>
                  <td>
                    <span [ngSwitch]="article.public">
                      <span *ngSwitchCase="0">密码</span>
                      <span *ngSwitchCase="1">公开</span>
                    </span>
                    <span *ngIf="article.public == -1">私密</span>
                  </td>
                  <td>
                    <span [ngSwitch]="article.state">
                      <span *ngSwitchCase="0">草稿</span>
                      <span *ngSwitchCase="1">已发布</span>
                    </span>
                    <span *ngIf="article.state == -1">回收站</span>
                  </td>
                  <td>
                    <div class="button-wrapper">
                      <a class="btn btn-success btn-sm" [routerLink]="['/article/edit/', article._id]">编辑文章</a>
                    </div>
                    <div class="button-wrapper" *ngIf="article.state == 0">
                      <button class="btn btn-warning btn-sm" 
                              (click)="moveToPublished([article._id])">快速发布</button>
                    </div>
                    <div class="button-wrapper" *ngIf="article.state == -1">
                      <button class="btn btn-warning btn-sm" 
                              (click)="moveToDraft([article._id])">恢复文章</button>
                    </div>
                    <div class="button-wrapper" *ngIf="article.state == 1">
                      <button class="btn btn-warning btn-sm" 
                              (click)="moveToDraft([article._id])">移到草稿</button>
                    </div>
                    <div class="button-wrapper" *ngIf="article.state != -1">
                      <button class="btn btn-danger btn-sm" 
                              (click)="moveToRecycle([article._id])">移回收站</button>
                    </div>
                    <div class="button-wrapper" *ngIf="article.state == -1">
                      <button class="btn btn-danger btn-sm" 
                              (click)="delArticleModal([article._id])">彻底删除</button>
                    </div>
                    <div class="button-wrapper">
                      <a class="btn btn-info btn-sm"
                         [href]="'//surmon.me/article/' + article.id"
                         target="_blank">查看文章</a>
                    </div>
                  </td>
                </tr>
              </ng-template>
            </tbody>
          </table>
           <br>
          <div class="text-center">
            <pagination class="pagination-xs"
                        firstText="首页"
                        lastText="末页"
                        nextText="下一页"
                        previousText="上一页"
                        pageBtnClass="btn-primary"
                        [totalItems]="articles.pagination.total"
                        [itemsPerPage]="articles.pagination.per_page"
                        [(ngModel)]="articles.pagination.current_page"
                        [maxSize]="7"
                        [boundaryLinks]="true"
                        [rotate]="false"
                        (pageChanged)="pageChanged($event)">
            </pagination>
          </div>
        </div>
      </div>
    </ba-card>
    <!-- 删除确认弹窗 -->
    <div bsModal #delModal="bs-modal" class="modal fade" tabindex="-1" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button class="close" aria-label="Close" (click)="cancelArticleModal()">
              <span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title">确认操作</h4>
          </div>
          <div class="modal-body">
            <div class="message">
              <span class="icon text-warning">
                <i class="ion-ios-information-outline"></i>
              </span>
              <span>确定要删除{{ del_articles ? '这篇' : '选中' }}文章吗？本操作不可逆</span>
            </div>
          </div>
          <div class="modal-footer">
            <button class="btn btn-primary confirm-btn" (click)="delArticles()">确认删除</button>
            <span>&nbsp;</span>
            <button class="btn btn-default confirm-btn" (click)="cancelArticleModal()">取消</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
